
 <!DOCTYPE HTML>
<html lang="zh-CN">
<head>
  <meta charset="UTF-8">
  
    <title>React-Redux使用笔记4--使用Redux管理状态 | Here. There.</title>
    <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=3, minimum-scale=1">
    
    <meta name="author" content="被删">
    
    <meta name="description" content="最近又重新拾起了React框架，并配合开源模板gentelella以及Redux建立了个简单的项目。《React-Redux使用笔记》系列用于记录过程中的一些使用和解决方法。本文记录使用Redux管理状态的过程。">
    
    
    
    
    <link rel="alternate" href="/atom.xml" title="Here. There." type="application/atom+xml">
    
    
    <link rel="icon" href="/img/favicon.ico">
    
    
    <link rel="apple-touch-icon" href="/img/pacman.jpg">
    <link rel="apple-touch-icon-precomposed" href="/img/pacman.jpg">
    
    <link rel="stylesheet" href="/css/style.css">
    
<script type="text/javascript">
var _hmt = _hmt || [];
(function() {
  var hm = document.createElement("script");
  hm.src = "//hm.baidu.com/hm.js?3d902de4a19cf2bf179534ffd2dd7b7f";
  var s = document.getElementsByTagName("script")[0]; 
  s.parentNode.insertBefore(hm, s);
})();
</script>

</head>

  <body>
    <header>
      <div>
		
			<div id="imglogo">
				<a href="/"><img src="/img/sun.png" alt="Here. There." title="Here. There."/></a>
			</div>
			
			<div id="textlogo">
				<h1 class="site-name"><a href="/" title="Here. There.">Here. There.</a></h1>
				<h2 class="blog-motto">Love ice cream. Love sunshine. Love life. Love the world. Love myself. Love you.</h2>
			</div>
			<div class="navbar"><a class="navbutton navmobile" href="#" title="菜单">
			</a></div>
			<nav class="animated">
				<ul>
					
						<li><a href="/">首页</a></li>
					
						<li><a href="https://github.com/godbasin/godbasin.github.io">所有文章</a></li>
					
						<li><a href="/archives">归档</a></li>
					
						<li><a href="/categories">分类</a></li>
					
						<li><a href="https://godbasin.github.io/front-end-playground">前端游乐场</a></li>
					
						<li><a href="/about">关于我</a></li>
					
				</ul>
			</nav>			
</div>

    </header>
    <div id="container">
      <div id="main" class="post" itemscope itemprop="blogPost">
	<article itemprop="articleBody"> 
		<header class="article-info clearfix">
  <h1 itemprop="name">
    
      <a href="/2017/01/02/react-redux-notes-4-use-redux/" title="React-Redux使用笔记4--使用Redux管理状态" itemprop="url">React-Redux使用笔记4--使用Redux管理状态</a>
  </h1>
  <p class="article-author">By
    
      <a href="https://godbasin.github.io" title="被删">被删</a>
    </p>
  <p class="article-time">
    <time datetime="2017-01-02T00:57:26.000Z" itemprop="datePublished">2017-01-02</time>
    更新日期:<time datetime="2018-12-18T15:34:58.344Z" itemprop="dateModified">2018-12-18</time>
    
  </p>
</header>
	<div class="article-content">
		
		
		<div id="toc" class="toc-article">
			<strong class="toc-title">文章目录</strong>
		<ol class="toc"><li class="toc-item toc-level-2"><a class="toc-link" href="#Redux"><span class="toc-number">1.</span> <span class="toc-text">Redux</span></a><ol class="toc-child"><li class="toc-item toc-level-3"><a class="toc-link" href="#三大原则"><span class="toc-number">1.1.</span> <span class="toc-text">三大原则</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#Action"><span class="toc-number">1.2.</span> <span class="toc-text">Action</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#Reducer"><span class="toc-number">1.3.</span> <span class="toc-text">Reducer</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#Store"><span class="toc-number">1.4.</span> <span class="toc-text">Store</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#数据流"><span class="toc-number">1.5.</span> <span class="toc-text">数据流</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#Redux和Flux"><span class="toc-number">1.6.</span> <span class="toc-text">Redux和Flux</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#参考"><span class="toc-number">1.7.</span> <span class="toc-text">参考</span></a></li></ol></li><li class="toc-item toc-level-2"><a class="toc-link" href="#React与Redux"><span class="toc-number">2.</span> <span class="toc-text">React与Redux</span></a><ol class="toc-child"><li class="toc-item toc-level-3"><a class="toc-link" href="#安装React-Redux"><span class="toc-number">2.1.</span> <span class="toc-text">安装React Redux</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#改造项目结构"><span class="toc-number">2.2.</span> <span class="toc-text">改造项目结构</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#创建actions"><span class="toc-number">2.3.</span> <span class="toc-text">创建actions</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#创建reducers"><span class="toc-number">2.4.</span> <span class="toc-text">创建reducers</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#更新index-js入口文件"><span class="toc-number">2.5.</span> <span class="toc-text">更新index.js入口文件</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#使用AppContainer作为APP外壳"><span class="toc-number">2.6.</span> <span class="toc-text">使用AppContainer作为APP外壳</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#Login组件添加action处理"><span class="toc-number">2.7.</span> <span class="toc-text">Login组件添加action处理</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#HomeContainer处理是否登陆"><span class="toc-number">2.8.</span> <span class="toc-text">HomeContainer处理是否登陆</span></a></li></ol></li><li class="toc-item toc-level-2"><a class="toc-link" href="#结束语"><span class="toc-number">3.</span> <span class="toc-text">结束语</span></a></li></ol>
		</div>
		
		<p>最近又重新拾起了React框架，并配合开源模板gentelella以及Redux建立了个简单的项目。《React-Redux使用笔记》系列用于记录过程中的一些使用和解决方法。<br>本文记录使用Redux管理状态的过程。<br><a id="more"></a></p>
<h2 id="Redux"><a href="#Redux" class="headerlink" title="Redux"></a>Redux</h2><hr>
<p>随着JavaScript单页应用开发日趋复杂，JavaScript需要管理比任何时候都要多的state（状态）。Redux试图让state的变化变得可预测。</p>
<h3 id="三大原则"><a href="#三大原则" class="headerlink" title="三大原则"></a>三大原则</h3><ul>
<li>单一数据源</li>
</ul>
<p>整个应用的state被储存在一棵object tree中，并且这个object tree只存在于唯一一个store中。</p>
<ul>
<li>State 是只读的</li>
</ul>
<p>惟一改变 state 的方法就是触发 action，action 是一个用于描述已发生事件的普通对象。</p>
<ul>
<li>使用纯函数来执行修改</li>
</ul>
<p>为了描述action如何改变state tree，你需要编写reducers。<br>Reducer只是一些纯函数，它接收先前的state和action，并返回新的state。</p>
<h3 id="Action"><a href="#Action" class="headerlink" title="Action"></a>Action</h3><ul>
<li>Action<br>Action是把数据从应用传到store的有效载荷。<br>它是store数据的唯一来源。一般来说你会通过store.dispatch()将action传到store。</li>
</ul>
<p>Action本质上是JavaScript普通对象。我们约定，action内必须使用一个字符串类型的type字段来表示将要执行的动作。<br>我们应该尽量减少在action中传递的数据。</p>
<ul>
<li>Action创建函数<br>Action创建函数就是生成action的方法。</li>
</ul>
<h3 id="Reducer"><a href="#Reducer" class="headerlink" title="Reducer"></a>Reducer</h3><p>Reducer指明应用如何更新state。</p>
<p>reducer就是一个纯函数，接收旧的state和action，返回新的state。</p>
<p>永远不要在reducer里做这些操作：</p>
<ul>
<li>修改传入参数</li>
<li>执行有副作用的操作，如API请求和路由跳转</li>
<li>调用非纯函数，如<code>Date.now()</code>或<code>Math.random()</code></li>
</ul>
<p>注意：</p>
<ul>
<li>不要修改state<blockquote>
<p>使用<code>Object.assign()</code>新建了一个副本。不能这样使用<code>Object.assign(state, { visibilityFilter: action.filter })</code>，因为它会改变第一个参数的值。你必须把第一个参数设置为空对象<br>你也可以开启对ES7提案对象展开运算符的支持, 从而使用<code>{ ...state, ...newState }</code>达到相同的目的</p>
</blockquote>
</li>
<li>在default情况下返回旧的state<blockquote>
<p>遇到未知的action时，一定要返回旧的state</p>
</blockquote>
</li>
</ul>
<h3 id="Store"><a href="#Store" class="headerlink" title="Store"></a>Store</h3><p>Store 就是把它们联系到一起的对象。</p>
<p>Store 有以下职责：</p>
<ul>
<li>维持应用的state</li>
<li>提供<code>getState()</code>方法获取state</li>
<li>提供<code>dispatch(action)</code>方法更新state</li>
<li>通过<code>subscribe(listener)</code>注册监听器</li>
<li>通过<code>subscribe(listener)</code>返回的函数注销监听器</li>
</ul>
<p>Redux应用只有一个单一的store。当需要拆分数据处理逻辑时，你应该使用reducer组合而不是创建多个store。</p>
<h3 id="数据流"><a href="#数据流" class="headerlink" title="数据流"></a>数据流</h3><p>严格的单向数据流是Redux架构的设计核心。</p>
<p>Redux应用中数据的生命周期遵循下面4个步骤：</p>
<ul>
<li>调用store.dispatch(action)</li>
<li>Redux store调用传入的reducer函数</li>
<li>根reducer应该把多个子reducer输出合并成一个单一的state树</li>
<li>Redux store保存了根reducer返回的完整state树</li>
</ul>
<h3 id="Redux和Flux"><a href="#Redux和Flux" class="headerlink" title="Redux和Flux"></a>Redux和Flux</h3><ul>
<li>store<blockquote>
<p>Redux只有一个store。Redux将各个store整合成一个完整的store，并且可以根据这个store推导出应用完整的 state。同时Redux中更新的逻辑也不在store中执行而是放在reducer中<br>Flux里面会有多个store存储应用数据，并在store里面执行更新逻辑，当store变化的时候再通知controller-view更新自己的数据</p>
</blockquote>
</li>
<li>没有Dispatcher<blockquote>
<p>Redux中没有Dispatcher的概念，它使用reducer来进行事件的处理，reducer是一个纯函数，这个函数被表述为<code>(previousState, action) =&gt; newState</code>，它根据应用的状态和当前的action推导出新的state</p>
</blockquote>
</li>
</ul>
<h3 id="参考"><a href="#参考" class="headerlink" title="参考"></a>参考</h3><p><a href="http://cn.redux.js.org/docs/react-redux/api.html" target="_blank" rel="external">react-redux中文文档</a></p>
<h2 id="React与Redux"><a href="#React与Redux" class="headerlink" title="React与Redux"></a>React与Redux</h2><hr>
<p>这次我们使用Redux来管理应用的状态。</p>
<h3 id="安装React-Redux"><a href="#安装React-Redux" class="headerlink" title="安装React Redux"></a>安装React Redux</h3><figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line">npm install --save react-redux</div></pre></td></tr></table></figure>
<h3 id="改造项目结构"><a href="#改造项目结构" class="headerlink" title="改造项目结构"></a>改造项目结构</h3><p>上了Redux后本骚年的目录结构如图：<br><img src="https://github-imglib-1255459943.cos.ap-chengdu.myqcloud.com/A99.tmp.png" alt="image"><br>可以看到，这里除了增加了Redux相关的actions、store相关文件，也将原有目录组织重新调整了一下。</p>
<ul>
<li>containers/AppContainer.jsx为整个应用外壳</li>
<li>routes会根据应用路由管理<ul>
<li>containers为该路由主外壳</li>
<li>modules为该路由的一些模块</li>
<li>components为该路由的一些组件</li>
</ul>
</li>
</ul>
<h3 id="创建actions"><a href="#创建actions" class="headerlink" title="创建actions"></a>创建actions</h3><p>在actions文件夹下新建commonActions.js文件：<br><figure class="highlight js"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div></pre></td><td class="code"><pre><div class="line"><span class="comment">// action 类型</span></div><div class="line"><span class="keyword">export</span> <span class="keyword">const</span> USER_NAME = <span class="string">'USER_NAME'</span></div><div class="line"></div><div class="line"><span class="comment">// action 创建函数</span></div><div class="line"><span class="keyword">export</span> <span class="function"><span class="keyword">function</span> <span class="title">setUserName</span>(<span class="params">state</span>) </span>&#123;</div><div class="line">    <span class="keyword">return</span> &#123; <span class="attr">type</span>: USER_NAME, state &#125;</div><div class="line">&#125;</div></pre></td></tr></table></figure></p>
<p>这里我们创建了一个设置用户名的action创建函数，其action的type是USER_NAME。</p>
<h3 id="创建reducers"><a href="#创建reducers" class="headerlink" title="创建reducers"></a>创建reducers</h3><p>在store文件夹下新建reducers.js文件：<br><figure class="highlight js"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div><div class="line">10</div><div class="line">11</div><div class="line">12</div><div class="line">13</div><div class="line">14</div><div class="line">15</div><div class="line">16</div><div class="line">17</div><div class="line">18</div><div class="line">19</div></pre></td><td class="code"><pre><div class="line"><span class="keyword">import</span> &#123; combineReducers &#125; <span class="keyword">from</span> <span class="string">'redux'</span></div><div class="line"><span class="keyword">import</span> &#123; USER_NAME &#125; <span class="keyword">from</span> <span class="string">'../actions/commonActions'</span></div><div class="line"></div><div class="line"><span class="comment">// userName的reducer用于改变userName的状态</span></div><div class="line"><span class="function"><span class="keyword">function</span> <span class="title">userName</span>(<span class="params">state = null, action</span>) </span>&#123;</div><div class="line">    <span class="keyword">switch</span> (action.type) &#123;</div><div class="line">        <span class="keyword">case</span> USER_NAME:</div><div class="line">            <span class="keyword">return</span> action.state</div><div class="line">        <span class="keyword">default</span>:</div><div class="line">            <span class="keyword">return</span> state</div><div class="line">    &#125;</div><div class="line">&#125;</div><div class="line"></div><div class="line"><span class="comment">// 合并多个reducers</span></div><div class="line"><span class="keyword">const</span> AppReducer = combineReducers(&#123;</div><div class="line">    userName</div><div class="line">&#125;)</div><div class="line"></div><div class="line"><span class="keyword">export</span> <span class="keyword">default</span> AppReducer</div></pre></td></tr></table></figure></p>
<h3 id="更新index-js入口文件"><a href="#更新index-js入口文件" class="headerlink" title="更新index.js入口文件"></a>更新index.js入口文件</h3><p>入口文件index.js如下：<br><figure class="highlight js"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div><div class="line">10</div><div class="line">11</div><div class="line">12</div><div class="line">13</div><div class="line">14</div><div class="line">15</div><div class="line">16</div></pre></td><td class="code"><pre><div class="line"><span class="keyword">import</span> React <span class="keyword">from</span> <span class="string">'react'</span></div><div class="line"><span class="keyword">import</span> ReactDOM <span class="keyword">from</span> <span class="string">'react-dom'</span></div><div class="line"><span class="keyword">import</span> &#123; createStore &#125; <span class="keyword">from</span> <span class="string">'redux'</span></div><div class="line"><span class="keyword">import</span> &#123; Provider &#125; <span class="keyword">from</span> <span class="string">'react-redux'</span></div><div class="line"><span class="keyword">import</span> AppContainer <span class="keyword">from</span> <span class="string">'./containers/AppContainer.jsx'</span></div><div class="line"><span class="keyword">import</span> AppReducer <span class="keyword">from</span> <span class="string">'./store/reducers'</span></div><div class="line"></div><div class="line"><span class="keyword">const</span> store = createStore(AppReducer)</div><div class="line"><span class="keyword">const</span> MOUNT_NODE = <span class="built_in">document</span>.getElementById(<span class="string">'root'</span>)</div><div class="line"></div><div class="line">ReactDOM.render( </div><div class="line">      <span class="xml"><span class="tag">&lt;<span class="name">Provider</span> <span class="attr">store</span> = <span class="string">&#123;</span> <span class="attr">store</span> &#125; &gt;</span></span></div><div class="line">        <span class="tag">&lt;<span class="name">AppContainer</span> /&gt;</span></div><div class="line">      <span class="tag">&lt;/<span class="name">Provider</span>&gt;</span>,</div><div class="line">        MOUNT_NODE</div><div class="line">)</div></pre></td></tr></table></figure></p>
<p>其中，<code>&lt;Provider store&gt;</code>使组件层级中的<code>connect()</code>方法都能够获得Redux store。正常情况下，你的根组件应该嵌套在<code>&lt;Provider&gt;</code>中才能使用<code>connect()</code>方法。</p>
<h3 id="使用AppContainer作为APP外壳"><a href="#使用AppContainer作为APP外壳" class="headerlink" title="使用AppContainer作为APP外壳"></a>使用AppContainer作为APP外壳</h3><p>在containers文件夹下新建AppContainer.jsx文件：<br><figure class="highlight jsx"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div><div class="line">10</div><div class="line">11</div><div class="line">12</div><div class="line">13</div><div class="line">14</div><div class="line">15</div><div class="line">16</div><div class="line">17</div><div class="line">18</div><div class="line">19</div><div class="line">20</div><div class="line">21</div><div class="line">22</div><div class="line">23</div><div class="line">24</div><div class="line">25</div></pre></td><td class="code"><pre><div class="line"><span class="keyword">import</span> React, &#123; Component, PropTypes &#125; <span class="keyword">from</span> <span class="string">'react'</span> <span class="comment">//react</span></div><div class="line"><span class="keyword">import</span> &#123; Router, Route, Link, hashHistory, IndexRedirect, useRouterHistory &#125; <span class="keyword">from</span> <span class="string">'react-router'</span></div><div class="line"><span class="keyword">import</span> &#123; createHistory, createHashHistory &#125; <span class="keyword">from</span> <span class="string">'history'</span></div><div class="line"><span class="keyword">import</span> &#123; createStore &#125; <span class="keyword">from</span> <span class="string">'redux'</span></div><div class="line"><span class="keyword">import</span> Login <span class="keyword">from</span> <span class="string">'../routes/login'</span></div><div class="line"><span class="keyword">import</span> HomeContainer <span class="keyword">from</span> <span class="string">'../routes/home/containers/HomeContainer.jsx'</span></div><div class="line"></div><div class="line"><span class="keyword">let</span> history = useRouterHistory(createHashHistory)()</div><div class="line"></div><div class="line"><span class="keyword">export</span> <span class="class"><span class="keyword">class</span> <span class="title">AppContainer</span> <span class="keyword">extends</span> <span class="title">Component</span> </span>&#123;</div><div class="line">  render() &#123;</div><div class="line">    <span class="comment">// 正常的react-router使用方式</span></div><div class="line">    <span class="keyword">return</span> (</div><div class="line">      &lt;Router history=&#123;history&#125; &gt;</div><div class="line">        &lt;Route path="/"&gt;</div><div class="line">          &lt;IndexRedirect to=&#123;"login"&#125; /&gt;</div><div class="line">          &lt;Route path="login" component=&#123;Login&#125; /&gt;</div><div class="line">          &lt;Route path="home" component=&#123;HomeContainer&#125; /&gt;</div><div class="line">        &lt;/Route&gt;</div><div class="line">      &lt;/Router&gt;</div><div class="line">    )</div><div class="line">  &#125;</div><div class="line">&#125;</div><div class="line"></div><div class="line">export default AppContainer</div></pre></td></tr></table></figure></p>
<h3 id="Login组件添加action处理"><a href="#Login组件添加action处理" class="headerlink" title="Login组件添加action处理"></a>Login组件添加action处理</h3><p>Login组件如下:<br><figure class="highlight jsx"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div><div class="line">10</div><div class="line">11</div><div class="line">12</div><div class="line">13</div><div class="line">14</div><div class="line">15</div><div class="line">16</div><div class="line">17</div><div class="line">18</div><div class="line">19</div><div class="line">20</div><div class="line">21</div><div class="line">22</div><div class="line">23</div><div class="line">24</div><div class="line">25</div><div class="line">26</div><div class="line">27</div><div class="line">28</div><div class="line">29</div><div class="line">30</div><div class="line">31</div><div class="line">32</div><div class="line">33</div><div class="line">34</div><div class="line">35</div><div class="line">36</div><div class="line">37</div><div class="line">38</div><div class="line">39</div><div class="line">40</div><div class="line">41</div><div class="line">42</div><div class="line">43</div><div class="line">44</div><div class="line">45</div><div class="line">46</div><div class="line">47</div><div class="line">48</div><div class="line">49</div><div class="line">50</div><div class="line">51</div><div class="line">52</div><div class="line">53</div><div class="line">54</div><div class="line">55</div><div class="line">56</div><div class="line">57</div><div class="line">58</div><div class="line">59</div><div class="line">60</div><div class="line">61</div></pre></td><td class="code"><pre><div class="line"><span class="keyword">import</span> React, &#123; Component, PropTypes &#125; <span class="keyword">from</span> <span class="string">'react'</span></div><div class="line"><span class="keyword">import</span> &#123; connect &#125; <span class="keyword">from</span> <span class="string">'react-redux'</span></div><div class="line"><span class="keyword">import</span> &#123; setUserName &#125; <span class="keyword">from</span> <span class="string">'../../actions/commonActions.js'</span></div><div class="line"></div><div class="line"><span class="keyword">export</span> <span class="class"><span class="keyword">class</span> <span class="title">Login</span> <span class="keyword">extends</span> <span class="title">Component</span> </span>&#123;</div><div class="line">  render() &#123;</div><div class="line">    <span class="keyword">return</span> (</div><div class="line">      &lt;div&gt;</div><div class="line">        &lt;a className="hiddenanchor" id="signup"&gt;&lt;/a&gt;</div><div class="line">        &lt;a className="hiddenanchor" id="signin"&gt;&lt;/a&gt;</div><div class="line"></div><div class="line">        &lt;div className="login_wrapper"&gt;</div><div class="line">          &lt;div className="animate form login_form"&gt;</div><div class="line">            &lt;section className="login_content"&gt;</div><div class="line">              &lt;form onKeyPress=&#123;(e) =&gt; &#123; if (e.key === 'Enter') &#123; this.loginSubmit(e) &#125; &#125; &#125;&gt;</div><div class="line">                &lt;h1&gt;管理系统&lt;/h1&gt;</div><div class="line">                &lt;div&gt;</div><div class="line">                  &lt;input type="text" className="form-control" placeholder="用户名" ref="username" required /&gt;</div><div class="line">                &lt;/div&gt;</div><div class="line">                &lt;div&gt;</div><div class="line">                  &lt;input type="password" className="form-control" placeholder="密码" ref="password" required /&gt;</div><div class="line">                &lt;/div&gt;</div><div class="line">                &lt;div&gt;</div><div class="line">                  &lt;a className="btn btn-default submit" onClick=&#123;(e) =&gt; &#123; this.loginSubmit(e) &#125; &#125;&gt;登录&lt;/a&gt;</div><div class="line">                &lt;/div&gt;</div><div class="line">                &lt;div className="clearfix"&gt;&lt;/div&gt;</div><div class="line">              &lt;/form&gt;</div><div class="line">            &lt;/section&gt;</div><div class="line">          &lt;/div&gt;</div><div class="line">        &lt;/div&gt;</div><div class="line">      &lt;/div&gt;</div><div class="line">    )</div><div class="line">  &#125;</div><div class="line"></div><div class="line">  loginSubmit() &#123;</div><div class="line">    const username = this.refs.username.value.trim()</div><div class="line">    const password = this.refs.password.value.trim()</div><div class="line">    // console.log(JSON.stringify(&#123;username,password&#125;).toString());</div><div class="line">    if (username === undefined || password === undefined) &#123;</div><div class="line">      Notify(&#123;</div><div class="line">        title: '账户或密码不能为空',</div><div class="line">        type: 'error'</div><div class="line">      &#125;)</div><div class="line">      return</div><div class="line">    &#125;</div><div class="line">    // dispatch setUserName的action，触发更新userName状态</div><div class="line">    this.props.dispatch(setUserName(username))</div><div class="line">    this.context.router.push('/home')</div><div class="line">  &#125;</div><div class="line"></div><div class="line">  componentDidMount() &#123;</div><div class="line">    $('body').attr('class', 'login')</div><div class="line">  &#125;</div><div class="line">&#125;</div><div class="line"></div><div class="line">Login.contextTypes = &#123;</div><div class="line">  router: React.PropTypes.object</div><div class="line">&#125;</div><div class="line"></div><div class="line">// 使用connect，只注入dispatch，不监听store</div><div class="line">export default connect()(Login)</div></pre></td></tr></table></figure></p>
<ul>
<li><code>connect([mapStateToProps], [mapDispatchToProps], [mergeProps], [options])</code><ul>
<li>连接React组件与Redux store</li>
<li>连接操作不会改变原来的组件类。反而返回一个新的已与Redux store连接的组件类</li>
</ul>
</li>
</ul>
<h3 id="HomeContainer处理是否登陆"><a href="#HomeContainer处理是否登陆" class="headerlink" title="HomeContainer处理是否登陆"></a>HomeContainer处理是否登陆</h3><p>在routes/home/containers文件夹下新建HomeContainer.jsx文件：<br><figure class="highlight jsx"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div><div class="line">10</div><div class="line">11</div><div class="line">12</div><div class="line">13</div><div class="line">14</div><div class="line">15</div><div class="line">16</div><div class="line">17</div><div class="line">18</div><div class="line">19</div><div class="line">20</div><div class="line">21</div><div class="line">22</div><div class="line">23</div><div class="line">24</div><div class="line">25</div><div class="line">26</div><div class="line">27</div><div class="line">28</div><div class="line">29</div><div class="line">30</div><div class="line">31</div><div class="line">32</div><div class="line">33</div><div class="line">34</div><div class="line">35</div><div class="line">36</div><div class="line">37</div><div class="line">38</div><div class="line">39</div><div class="line">40</div><div class="line">41</div><div class="line">42</div><div class="line">43</div><div class="line">44</div><div class="line">45</div><div class="line">46</div><div class="line">47</div><div class="line">48</div><div class="line">49</div><div class="line">50</div><div class="line">51</div></pre></td><td class="code"><pre><div class="line"><span class="keyword">import</span> React, &#123; Component, PropTypes &#125; <span class="keyword">from</span> <span class="string">'react'</span></div><div class="line"><span class="keyword">import</span> &#123; connect &#125; <span class="keyword">from</span> <span class="string">'react-redux'</span></div><div class="line"></div><div class="line"><span class="keyword">export</span> <span class="class"><span class="keyword">class</span> <span class="title">HomeContainer</span> <span class="keyword">extends</span> <span class="title">Component</span> </span>&#123;</div><div class="line">  render() &#123;</div><div class="line">    <span class="keyword">const</span> that = <span class="keyword">this</span></div><div class="line">    <span class="keyword">return</span> (</div><div class="line">      <span class="xml"><span class="tag">&lt;<span class="name">div</span> <span class="attr">className</span>=<span class="string">"container body"</span>&gt;</span></span></div><div class="line">        <span class="tag">&lt;<span class="name">div</span> <span class="attr">className</span>=<span class="string">"main_container"</span>&gt;</span></div><div class="line">          <span class="tag">&lt;<span class="name">div</span> <span class="attr">className</span>=<span class="string">"right_col"</span> <span class="attr">role</span>=<span class="string">"main"</span>&gt;</span></div><div class="line">            Hello World!</div><div class="line">          <span class="tag">&lt;/<span class="name">div</span>&gt;</span></div><div class="line"></div><div class="line">          <span class="tag">&lt;<span class="name">footer</span>&gt;</span></div><div class="line">            <span class="tag">&lt;<span class="name">div</span> <span class="attr">className</span>=<span class="string">"pull-right"</span>&gt;</span></div><div class="line">              @godbasin</div><div class="line">          <span class="tag">&lt;/<span class="name">div</span>&gt;</span></div><div class="line">            <span class="tag">&lt;<span class="name">div</span> <span class="attr">className</span>=<span class="string">"clearfix"</span>&gt;</span><span class="tag">&lt;/<span class="name">div</span>&gt;</span></div><div class="line">          <span class="tag">&lt;/<span class="name">footer</span>&gt;</span></div><div class="line">        <span class="tag">&lt;/<span class="name">div</span>&gt;</span></div><div class="line">      <span class="tag">&lt;/<span class="name">div</span>&gt;</span></div><div class="line">    )</div><div class="line">  &#125;</div><div class="line">  componentDidMount() &#123;</div><div class="line">    <span class="comment">// 判断如果未登陆则跳转</span></div><div class="line">    <span class="keyword">if</span> (<span class="keyword">this</span>.props.getUserName() === <span class="literal">null</span>) &#123;</div><div class="line">      Notify(&#123;</div><div class="line">        <span class="attr">title</span>: <span class="string">'请先登录'</span>,</div><div class="line">        <span class="attr">type</span>: <span class="string">'error'</span></div><div class="line">      &#125;)</div><div class="line">      <span class="keyword">this</span>.context.router.push(<span class="string">'/login'</span>)</div><div class="line">    &#125;</div><div class="line">    $(<span class="string">'body'</span>).attr(<span class="string">'class'</span>, <span class="string">'nav-md'</span>)</div><div class="line">    $(<span class="string">'.right_col'</span>).css(<span class="string">'min-height'</span>, $(<span class="built_in">window</span>).height())</div><div class="line">  &#125;</div><div class="line">&#125;</div><div class="line"></div><div class="line">HomeContainer.contextTypes = &#123;</div><div class="line">  <span class="attr">router</span>: React.PropTypes.object</div><div class="line">&#125;</div><div class="line"></div><div class="line"><span class="function"><span class="keyword">function</span> <span class="title">connectState</span>(<span class="params">state</span>) </span>&#123;</div><div class="line">  <span class="keyword">return</span> &#123;</div><div class="line">    getUserName() &#123;</div><div class="line">      <span class="keyword">return</span> state.userName</div><div class="line">    &#125;</div><div class="line">  &#125;</div><div class="line">&#125;</div><div class="line"></div><div class="line"><span class="comment">// 使用connect，注入dispatch和userName</span></div><div class="line"><span class="keyword">export</span> <span class="keyword">default</span> connect(connectState)(HomeContainer)</div></pre></td></tr></table></figure></p>
<p>至此，我们大概完成了Redux的状态管理，并在登录中使用。</p>
<h2 id="结束语"><a href="#结束语" class="headerlink" title="结束语"></a>结束语</h2><hr>
<p>其实，Redux和React之间没有关系。Redux 支持 React、Angular、Ember、jQuery甚至纯JavaScript，不过这两者搭配的效果也很棒棒的。关于Redux还有更多需要研究的，小伙伴们下来也可以多多琢磨呢。<br><a href="https://github.com/godbasin/godbasin.github.io/tree/blog-codes/react-redux-notes/4-use-redux" target="_blank" rel="external">此处查看项目代码</a><br><a href="http://react-redux-notes.godbasin.com/4-use-redux/index.html" target="_blank" rel="external">此处查看页面效果</a></p>
  
		
		<p style="margin-top:50px;">
			查看Github有更多内容噢：<a href="https://github.com/godbasin">https://github.com/godbasin</a>
			<br>
			更欢迎来<a href="https://godbasin.github.io/front-end-playground">被删的前端游乐场</a>边撸猫边学前端噢
		</p>
	</div>
	<img src="https://github-imglib-1255459943.cos.ap-chengdu.myqcloud.com/2code2.jpg" width="200" height="200" style="display:block;margin: 0 auto;" />
	<p style="text-align: center;margin-top: 10px;margin-bottom: 20px;">码生艰难，写文不易，给我家猪囤点猫粮了喵~</p>
	<div class="author-right">
  		<p>作者：被删</p>
  		<p>出处：<a href="https://godbasin.github.io">https://godbasin.github.io</a></p>
  		<p>本文版权归作者所有，欢迎转载，但未经作者同意必须保留此段声明，且在文章页面明显位置给出原文连接，否则保留追究法律责任的权利。</p>
	</div>
		<footer class="article-footer clearfix">

  <div class="article-tags">
  
  <span></span> <a href="/tags/笔记/">笔记</a>
  </div>


<div class="article-categories">
  <span></span>
  <a class="article-category-link" href="/categories/react沙拉/">react沙拉</a>
</div>



<div class="article-share" id="share">

  
<!-- JiaThis Button BEGIN -->
<div class="jiathis_style_24x24">
	<a class="jiathis_button_qzone"></a>
	<a class="jiathis_button_tsina"></a>
	<a class="jiathis_button_tqq"></a>
	<a class="jiathis_button_weixin"></a>
	<a class="jiathis_button_renren"></a>
	<a href="http://www.jiathis.com/share?uid=2134021" class="jiathis jiathis_txt jtico jtico_jiathis" target="_blank"></a>
</div>
<script type="text/javascript">
var jiathis_config = {data_track_clickback:'true'};
</script>
<script type="text/javascript" src="http://v3.jiathis.com/code_mini/jia.js?uid=2134021" charset="utf-8"></script>
<!-- JiaThis Button END -->   


</div>
</footer>   	       
	</article>
	
<nav class="article-nav clearfix">
 
 <div class="prev" >
 <a href="/2017/01/08/react-redux-notes-5-create-top/" title="React-Redux使用笔记5--创建Top组件">
  <strong>PREVIOUS:</strong><br/>
  <span>
  React-Redux使用笔记5--创建Top组件</span>
</a>
</div>


<div class="next">
<a href="/2017/01/01/react-redux-notes-3-use-router-login/"  title="React-Redux使用笔记3--使用router完成简单登陆功能">
 <strong>NEXT:</strong><br/> 
 <span>React-Redux使用笔记3--使用router完成简单登陆功能
</span>
</a>
</div>

</nav>

	<!-- 如果不是首页且没使用 `comments: false` 关闭评论，则尝试加载评论 -->

    <!-- 配置中启用多说时，导入相应代码 -->
    


</div>  
      <div class="openaside"><a class="navbutton" href="#" title="显示侧边栏"></a></div>

  <div id="toc" class="toc-aside">
  <strong class="toc-title">文章目录</strong>
  <ol class="toc"><li class="toc-item toc-level-2"><a class="toc-link" href="#Redux"><span class="toc-number">1.</span> <span class="toc-text">Redux</span></a><ol class="toc-child"><li class="toc-item toc-level-3"><a class="toc-link" href="#三大原则"><span class="toc-number">1.1.</span> <span class="toc-text">三大原则</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#Action"><span class="toc-number">1.2.</span> <span class="toc-text">Action</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#Reducer"><span class="toc-number">1.3.</span> <span class="toc-text">Reducer</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#Store"><span class="toc-number">1.4.</span> <span class="toc-text">Store</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#数据流"><span class="toc-number">1.5.</span> <span class="toc-text">数据流</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#Redux和Flux"><span class="toc-number">1.6.</span> <span class="toc-text">Redux和Flux</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#参考"><span class="toc-number">1.7.</span> <span class="toc-text">参考</span></a></li></ol></li><li class="toc-item toc-level-2"><a class="toc-link" href="#React与Redux"><span class="toc-number">2.</span> <span class="toc-text">React与Redux</span></a><ol class="toc-child"><li class="toc-item toc-level-3"><a class="toc-link" href="#安装React-Redux"><span class="toc-number">2.1.</span> <span class="toc-text">安装React Redux</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#改造项目结构"><span class="toc-number">2.2.</span> <span class="toc-text">改造项目结构</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#创建actions"><span class="toc-number">2.3.</span> <span class="toc-text">创建actions</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#创建reducers"><span class="toc-number">2.4.</span> <span class="toc-text">创建reducers</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#更新index-js入口文件"><span class="toc-number">2.5.</span> <span class="toc-text">更新index.js入口文件</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#使用AppContainer作为APP外壳"><span class="toc-number">2.6.</span> <span class="toc-text">使用AppContainer作为APP外壳</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#Login组件添加action处理"><span class="toc-number">2.7.</span> <span class="toc-text">Login组件添加action处理</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#HomeContainer处理是否登陆"><span class="toc-number">2.8.</span> <span class="toc-text">HomeContainer处理是否登陆</span></a></li></ol></li><li class="toc-item toc-level-2"><a class="toc-link" href="#结束语"><span class="toc-number">3.</span> <span class="toc-text">结束语</span></a></li></ol>
  </div>

<div id="asidepart">
<div class="closeaside"><a class="closebutton" href="#" title="隐藏侧边栏"></a></div>
<aside class="clearfix">

  	
	<div class="archiveslist">
		<p class="asidetitle">最近文章</p>
			<ul class="archive-list">
			
					
				<li class="archive-list-item">
					<a class="archive-list-link" href="/2019/11/15/wxapp-latest-20191115/" title="小程序开发月刊第11期（20191115）">小程序开发月刊第11期（201911...</a>
				</li>
				
			
					
				<li class="archive-list-item">
					<a class="archive-list-link" href="/2019/11/10/change-log/" title="前端 CHANGELOG 生成指南">前端 CHANGELOG 生成指南...</a>
				</li>
				
			
					
				<li class="archive-list-item">
					<a class="archive-list-link" href="/2019/11/09/wxapp-global-data-behavior/" title="小程序的奇技淫巧 -- globalDataBehavior管理全局状态">小程序的奇技淫巧 -- global...</a>
				</li>
				
			
					
				<li class="archive-list-item">
					<a class="archive-list-link" href="/2019/10/15/wxapp-latest-20191015/" title="小程序开发月刊第十期（20191015）">小程序开发月刊第十期（2019101...</a>
				</li>
				
			
					
				<li class="archive-list-item">
					<a class="archive-list-link" href="/2019/10/13/about-front-end-3-growth/" title="前端这几年--3.关于成长和焦虑">前端这几年--3.关于成长和焦虑...</a>
				</li>
				
			
					
				<li class="archive-list-item">
					<a class="archive-list-link" href="/2019/09/16/wxapp-latest-20190916/" title="小程序开发月刊第九期（20190916）">小程序开发月刊第九期（2019091...</a>
				</li>
				
			
					
				<li class="archive-list-item">
					<a class="archive-list-link" href="/2019/08/15/wxapp-latest-20190815/" title="小程序开发月刊第八期（20190815）">小程序开发月刊第八期（2019081...</a>
				</li>
				
			
					
				<li class="archive-list-item">
					<a class="archive-list-link" href="/2019/07/27/front-end-playground/" title="被删的前端游乐场建成！">被删的前端游乐场建成！</a>
				</li>
				
			
					
				<li class="archive-list-item">
					<a class="archive-list-link" href="/2019/07/21/vue-for-everyone-3/" title="9102全员学Vue--3.把页面拼成个Web应用">9102全员学Vue--3.把页面拼...</a>
				</li>
				
			
					
				<li class="archive-list-item">
					<a class="archive-list-link" href="/2019/07/15/wxapp-latest-20190715/" title="小程序开发月刊第七期（20190715）">小程序开发月刊第七期（2019071...</a>
				</li>
				
			
				
			
				
			
				
			
				
			
				
			
				
			
				
			
				
			
				
			
				
			
				
			
				
			
				
			
				
			
				
			
				
			
				
			
				
			
				
			
				
			
				
			
				
			
				
			
				
			
				
			
				
			
				
			
				
			
				
			
				
			
				
			
				
			
				
			
				
			
				
			
				
			
				
			
				
			
				
			
				
			
				
			
				
			
				
			
				
			
				
			
				
			
				
			
				
			
				
			
				
			
				
			
				
			
				
			
				
			
				
			
				
			
				
			
				
			
				
			
				
			
				
			
				
			
				
			
				
			
				
			
				
			
				
			
				
			
				
			
				
			
				
			
				
			
				
			
				
			
				
			
				
			
				
			
				
			
				
			
				
			
				
			
				
			
				
			
				
			
				
			
				
			
				
			
				
			
				
			
				
			
				
			
				
			
				
			
				
			
				
			
				
			
				
			
				
			
				
			
				
			
				
			
				
			
				
			
				
			
				
			
				
			
				
			
				
			
				
			
				
			
				
			
				
			
				
			
				
			
				
			
				
			
				
			
				
			
				
			
				
			
				
			
				
			
				
			
				
			
				
			
				
			
				
			
				
			
				
			
				
			
				
			
				
			
				
			
				
			
				
			
				
			
				
			
				
			
				
			
				
			
				
			
				
			
				
			
				
			
				
			
				
			
				
			
				
			
				
			
				
			
				
			
				
			
				
			
				
			
				
			
				
			
				
			
				
			
				
			
				
			
				
			
				
			
				
			
				
			
				
			
				
			
				
			
				
			
				
			
				
			
				
			
				
			
				
			
				
			
				
			
				
			
				
			
				
			
				
			
				
			
				
			
				
			
				
			
				
			
				
			
				
			
				
			
				
			
				
			
				
			
				
			
				
			
				
			
				
			
				
			
				
			
				
			
				
			
				
			
				
			
				
			
				
			
				
			
				
			
				
			
				
			
				
			
				
			
				
			
				
			
				
			
				
			
				
			
				
			
				
			
				
			
				
			
				
			
				
			
				
			
				
			
				
			
				
			
				
			
				
			
				
			
				
			
				
			
				
			
				
			
				
			
				
			
				
			
				
			
				
			
				
			
				
			
				
			
				
			
				
			
				
			
				
			
				
			
				
			
				
			
				
			
				
			
				
			
				
			
				
			
				
			
				
			
			</ul>
	</div>


  
  <div class="archiveslist">
    <p class="asidetitle"><a href="/archives">归档</a></p>
      <ul class="archive-list"><li class="archive-list-item"><a class="archive-list-link" href="/archives/2019/11/">十一月 2019</a><span class="archive-list-count">3</span></li><li class="archive-list-item"><a class="archive-list-link" href="/archives/2019/10/">十月 2019</a><span class="archive-list-count">2</span></li><li class="archive-list-item"><a class="archive-list-link" href="/archives/2019/09/">九月 2019</a><span class="archive-list-count">1</span></li><li class="archive-list-item"><a class="archive-list-link" href="/archives/2019/08/">八月 2019</a><span class="archive-list-count">1</span></li><li class="archive-list-item"><a class="archive-list-link" href="/archives/2019/07/">七月 2019</a><span class="archive-list-count">4</span></li><li class="archive-list-item"><a class="archive-list-link" href="/archives/2019/06/">六月 2019</a><span class="archive-list-count">4</span></li><li class="archive-list-item"><a class="archive-list-link" href="/archives/2019/05/">五月 2019</a><span class="archive-list-count">1</span></li><li class="archive-list-item"><a class="archive-list-link" href="/archives/2019/04/">四月 2019</a><span class="archive-list-count">2</span></li><li class="archive-list-item"><a class="archive-list-link" href="/archives/2019/03/">三月 2019</a><span class="archive-list-count">2</span></li><li class="archive-list-item"><a class="archive-list-link" href="/archives/2019/02/">二月 2019</a><span class="archive-list-count">2</span></li><li class="archive-list-item"><a class="archive-list-link" href="/archives/2019/01/">一月 2019</a><span class="archive-list-count">3</span></li><li class="archive-list-item"><a class="archive-list-link" href="/archives/2018/12/">十二月 2018</a><span class="archive-list-count">4</span></li><li class="archive-list-item"><a class="archive-list-link" href="/archives/2018/11/">十一月 2018</a><span class="archive-list-count">4</span></li><li class="archive-list-item"><a class="archive-list-link" href="/archives/2018/10/">十月 2018</a><span class="archive-list-count">1</span></li><li class="archive-list-item"><a class="archive-list-link" href="/archives/2018/09/">九月 2018</a><span class="archive-list-count">3</span></li><li class="archive-list-item"><a class="archive-list-link" href="/archives/2018/08/">八月 2018</a><span class="archive-list-count">2</span></li><li class="archive-list-item"><a class="archive-list-link" href="/archives/2018/07/">七月 2018</a><span class="archive-list-count">3</span></li><li class="archive-list-item"><a class="archive-list-link" href="/archives/2018/06/">六月 2018</a><span class="archive-list-count">3</span></li><li class="archive-list-item"><a class="archive-list-link" href="/archives/2018/05/">五月 2018</a><span class="archive-list-count">5</span></li><li class="archive-list-item"><a class="archive-list-link" href="/archives/2018/04/">四月 2018</a><span class="archive-list-count">4</span></li><li class="archive-list-item"><a class="archive-list-link" href="/archives/2018/03/">三月 2018</a><span class="archive-list-count">7</span></li><li class="archive-list-item"><a class="archive-list-link" href="/archives/2018/02/">二月 2018</a><span class="archive-list-count">4</span></li><li class="archive-list-item"><a class="archive-list-link" href="/archives/2018/01/">一月 2018</a><span class="archive-list-count">9</span></li><li class="archive-list-item"><a class="archive-list-link" href="/archives/2017/12/">十二月 2017</a><span class="archive-list-count">5</span></li><li class="archive-list-item"><a class="archive-list-link" href="/archives/2017/11/">十一月 2017</a><span class="archive-list-count">3</span></li><li class="archive-list-item"><a class="archive-list-link" href="/archives/2017/10/">十月 2017</a><span class="archive-list-count">5</span></li><li class="archive-list-item"><a class="archive-list-link" href="/archives/2017/09/">九月 2017</a><span class="archive-list-count">6</span></li><li class="archive-list-item"><a class="archive-list-link" href="/archives/2017/08/">八月 2017</a><span class="archive-list-count">11</span></li><li class="archive-list-item"><a class="archive-list-link" href="/archives/2017/07/">七月 2017</a><span class="archive-list-count">9</span></li><li class="archive-list-item"><a class="archive-list-link" href="/archives/2017/06/">六月 2017</a><span class="archive-list-count">10</span></li><li class="archive-list-item"><a class="archive-list-link" href="/archives/2017/05/">五月 2017</a><span class="archive-list-count">15</span></li><li class="archive-list-item"><a class="archive-list-link" href="/archives/2017/04/">四月 2017</a><span class="archive-list-count">7</span></li><li class="archive-list-item"><a class="archive-list-link" href="/archives/2017/03/">三月 2017</a><span class="archive-list-count">10</span></li><li class="archive-list-item"><a class="archive-list-link" href="/archives/2017/02/">二月 2017</a><span class="archive-list-count">41</span></li><li class="archive-list-item"><a class="archive-list-link" href="/archives/2017/01/">一月 2017</a><span class="archive-list-count">6</span></li><li class="archive-list-item"><a class="archive-list-link" href="/archives/2016/12/">十二月 2016</a><span class="archive-list-count">7</span></li><li class="archive-list-item"><a class="archive-list-link" href="/archives/2016/11/">十一月 2016</a><span class="archive-list-count">9</span></li><li class="archive-list-item"><a class="archive-list-link" href="/archives/2016/10/">十月 2016</a><span class="archive-list-count">5</span></li><li class="archive-list-item"><a class="archive-list-link" href="/archives/2016/09/">九月 2016</a><span class="archive-list-count">7</span></li><li class="archive-list-item"><a class="archive-list-link" href="/archives/2016/08/">八月 2016</a><span class="archive-list-count">9</span></li><li class="archive-list-item"><a class="archive-list-link" href="/archives/2016/07/">七月 2016</a><span class="archive-list-count">14</span></li><li class="archive-list-item"><a class="archive-list-link" href="/archives/2016/06/">六月 2016</a><span class="archive-list-count">9</span></li></ul>
  </div>


  
<div class="archiveslist">
	<p class="asidetitle"><a href="/categories">分类</a></p>
		<ul class="archive-list">
		
			<li class="archive-list-item">
				<a class="archive-list-link" href="/categories/CSS炒饭/" title="CSS炒饭">CSS炒饭<sup>3</sup></a>
			</li>
		
			<li class="archive-list-item">
				<a class="archive-list-link" href="/categories/D3小馒头/" title="D3小馒头">D3小馒头<sup>8</sup></a>
			</li>
		
			<li class="archive-list-item">
				<a class="archive-list-link" href="/categories/angular2火锅/" title="angular2火锅">angular2火锅<sup>25</sup></a>
			</li>
		
			<li class="archive-list-item">
				<a class="archive-list-link" href="/categories/angular混搭/" title="angular混搭">angular混搭<sup>33</sup></a>
			</li>
		
			<li class="archive-list-item">
				<a class="archive-list-link" href="/categories/box2djs方糖/" title="box2djs方糖">box2djs方糖<sup>34</sup></a>
			</li>
		
			<li class="archive-list-item">
				<a class="archive-list-link" href="/categories/cyclejs哈根达斯/" title="cyclejs哈根达斯">cyclejs哈根达斯<sup>8</sup></a>
			</li>
		
			<li class="archive-list-item">
				<a class="archive-list-link" href="/categories/jQuery杂烩/" title="jQuery杂烩">jQuery杂烩<sup>3</sup></a>
			</li>
		
			<li class="archive-list-item">
				<a class="archive-list-link" href="/categories/js什锦/" title="js什锦">js什锦<sup>26</sup></a>
			</li>
		
			<li class="archive-list-item">
				<a class="archive-list-link" href="/categories/react沙拉/" title="react沙拉">react沙拉<sup>16</sup></a>
			</li>
		
			<li class="archive-list-item">
				<a class="archive-list-link" href="/categories/three-js奶茶/" title="three.js奶茶">three.js奶茶<sup>5</sup></a>
			</li>
		
			<li class="archive-list-item">
				<a class="archive-list-link" href="/categories/vue八宝粥/" title="vue八宝粥">vue八宝粥<sup>29</sup></a>
			</li>
		
			<li class="archive-list-item">
				<a class="archive-list-link" href="/categories/webpack宾治/" title="webpack宾治">webpack宾治<sup>9</sup></a>
			</li>
		
			<li class="archive-list-item">
				<a class="archive-list-link" href="/categories/web乱炖/" title="web乱炖">web乱炖<sup>2</sup></a>
			</li>
		
			<li class="archive-list-item">
				<a class="archive-list-link" href="/categories/前端满汉全席/" title="前端满汉全席">前端满汉全席<sup>8</sup></a>
			</li>
		
			<li class="archive-list-item">
				<a class="archive-list-link" href="/categories/喵/" title="喵">喵<sup>2</sup></a>
			</li>
		
			<li class="archive-list-item">
				<a class="archive-list-link" href="/categories/小程序双皮奶/" title="小程序双皮奶">小程序双皮奶<sup>27</sup></a>
			</li>
		
			<li class="archive-list-item">
				<a class="archive-list-link" href="/categories/工作这杯茶/" title="工作这杯茶">工作这杯茶<sup>5</sup></a>
			</li>
		
			<li class="archive-list-item">
				<a class="archive-list-link" href="/categories/应用巧克力盒/" title="应用巧克力盒">应用巧克力盒<sup>2</sup></a>
			</li>
		
			<li class="archive-list-item">
				<a class="archive-list-link" href="/categories/思想棉花糖/" title="思想棉花糖">思想棉花糖<sup>8</sup></a>
			</li>
		
			<li class="archive-list-item">
				<a class="archive-list-link" href="/categories/柴米油盐工具集/" title="柴米油盐工具集">柴米油盐工具集<sup>1</sup></a>
			</li>
		
			<li class="archive-list-item">
				<a class="archive-list-link" href="/categories/自动化甜筒/" title="自动化甜筒">自动化甜筒<sup>1</sup></a>
			</li>
		
			<li class="archive-list-item">
				<a class="archive-list-link" href="/categories/非前端钙片/" title="非前端钙片">非前端钙片<sup>7</sup></a>
			</li>
		
		</ul>
</div>


  
<div class="archiveslist">
	<p class="asidetitle">标签</p>
		<ul class="archive-list">
		
			<li class="archive-list-item">
				<a class="archive-list-link" href="/tags/分享/" title="分享">分享<sup>44</sup></a>
			</li>
		
			<li class="archive-list-item">
				<a class="archive-list-link" href="/tags/原创部件/" title="原创部件">原创部件<sup>1</sup></a>
			</li>
		
			<li class="archive-list-item">
				<a class="archive-list-link" href="/tags/心态/" title="心态">心态<sup>5</sup></a>
			</li>
		
			<li class="archive-list-item">
				<a class="archive-list-link" href="/tags/教程/" title="教程">教程<sup>73</sup></a>
			</li>
		
			<li class="archive-list-item">
				<a class="archive-list-link" href="/tags/晒猫/" title="晒猫">晒猫<sup>2</sup></a>
			</li>
		
			<li class="archive-list-item">
				<a class="archive-list-link" href="/tags/笔记/" title="笔记">笔记<sup>121</sup></a>
			</li>
		
			<li class="archive-list-item">
				<a class="archive-list-link" href="/tags/算法/" title="算法">算法<sup>9</sup></a>
			</li>
		
			<li class="archive-list-item">
				<a class="archive-list-link" href="/tags/自制插件/" title="自制插件">自制插件<sup>2</sup></a>
			</li>
		
			<li class="archive-list-item">
				<a class="archive-list-link" href="/tags/逻辑实现/" title="逻辑实现">逻辑实现<sup>5</sup></a>
			</li>
		
		</ul>
</div>


    <div class="archiveslist">
    <p class="asidetitle"><a href="/archives">about</a></p>
      <ul class="archive-list">
      	<li class="archive-list-item">
      		<a>wangbeishan@163.com</a>
      		<a href="https://github.com/godbasin">github.com/godbasin</a>
      	</li>
      </ul>
  </div>

  <div class="rsspart">
	<a href="/atom.xml" target="_blank" title="rss">RSS 订阅</a>
</div>

</aside>
</div>
    </div>
    <footer><div id="footer" >
	
	<section class="info">
		<p> 即使梦想再小，却很伟大 ^_^ </p>
	</section>
	 
		<p class="copyright">Powered by <a href="http://hexo.io" target="_blank" title="hexo">hexo</a> and Theme by <a href="https://github.com/A-limon/pacman" target="_blank" title="Pacman">Pacman</a> © 2019 
		
		<a href="https://godbasin.github.io" target="_blank" title="被删">被删</a>
		
		</p>
</div>
</footer>
    <script src="/js/jquery-2.1.0.min.js"></script>
<script type="text/javascript">
$(document).ready(function(){ 
  $('.navbar').click(function(){
    $('header nav').toggleClass('shownav');
  });
  var myWidth = 0;
  function getSize(){
    if( typeof( window.innerWidth ) == 'number' ) {
      myWidth = window.innerWidth;
    } else if( document.documentElement && document.documentElement.clientWidth) {
      myWidth = document.documentElement.clientWidth;
    };
  };
  var m = $('#main'),
      a = $('#asidepart'),
      c = $('.closeaside'),
      o = $('.openaside');
  $(window).resize(function(){
    getSize(); 
    if (myWidth >= 1024) {
      $('header nav').removeClass('shownav');
    }else
    {
      m.removeClass('moveMain');
      a.css('display', 'block').removeClass('fadeOut');
      o.css('display', 'none');
      
      $('#toc.toc-aside').css('display', 'none');
        
    }
  });
  c.click(function(){
    a.addClass('fadeOut').css('display', 'none');
    o.css('display', 'block').addClass('fadeIn');
    m.addClass('moveMain');
  });
  o.click(function(){
    o.css('display', 'none').removeClass('beforeFadeIn');
    a.css('display', 'block').removeClass('fadeOut').addClass('fadeIn');      
    m.removeClass('moveMain');
  });
  $(window).scroll(function(){
    o.css("top",Math.max(80,260-$(this).scrollTop()));
  });
});
</script>

<script type="text/javascript">
$(document).ready(function(){ 
  var ai = $('.article-content>iframe'),
      ae = $('.article-content>embed'),
      t  = $('#toc'),
      h  = $('article h2')
      ah = $('article h2'),
      ta = $('#toc.toc-aside'),
      o  = $('.openaside'),
      c  = $('.closeaside');
  if(ai.length>0){
    ai.wrap('<div class="video-container" />');
  };
  if(ae.length>0){
   ae.wrap('<div class="video-container" />');
  };
  if(ah.length==0){
    t.css('display','none');
  }else{
    c.click(function(){
      ta.css('display', 'block').addClass('fadeIn');
    });
    o.click(function(){
      ta.css('display', 'none');
    });
    $(window).scroll(function(){
      ta.css("top",Math.max(140,320-$(this).scrollTop()));
    });
  };
});
</script>







  </body>
</html>
